import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/home/index.vue'
import Register from '../views/register/register.vue'
import Login from '../views/register/login.vue'
import Shopping from '../views/shopping/shopping.vue'
import Order from '../views/order/order.vue'
import Mine from '../views/mine/mine.vue'
import Merchant from '../views/Merchant/Merchant.vue'
import Address from '../views/address/address.vue'
import AddressEdit from '../views/address/addressEdit.vue'
import Modificationaddress from '../views/address/modificationaddress.vue'
import Submitorder from '../views/order/submitorder.vue'
const routes = [
    {
        path: '/',
        name: 'home',
        component: Home
    },
    {
        path: '/register',
        name: 'register',
        component: Register,
        meta: {
            auth: true
        }
    },
    {
        path: '/login',
        name: 'login',
        component: Login,
        meta: {
            auth: true
        }
    },
    {
        path: "/detail/:id",
        name: "Detail",
        component: function () {
            return import(/* webpackChunkName: "Detail" */ "../views/Detail/Detail.vue")
        },
    },
    {
        path: '/shopping',
        name: 'shopping',
        component: Shopping
        // 局部守卫
        // beforeEnter: ((to, form, next) => {
        //     if (!to.meta.auth) {
        //         let isLogin = sessionStorage.getItem("token");
        //         if (!isLogin) {
        //             router.push("/login");
        //         } else {
        //             next();
        //         }
        //     } else {
        //         console.log("正常跳转");
        //         next();
        //     }


        // })
    },
    {
        path: '/order',
        name: 'order',
        component: Order
    },
    {
        path: '/mine',
        name: 'mine',
        component: Mine
    },
    {
        path: '/Merchant',
        name: 'Merchant',
        component: Merchant
    },
    {
        path: '/address',
        name: 'address',
        component: Address
    },
    {
        path: '/addressEdit',
        name: 'addressEdit',
        component: AddressEdit
    },
    {
        path: '/modificationaddress',
        name: 'modificationaddress',
        component: Modificationaddress
    },
    {
        path: '/submitorder',
        name: 'Submitorder',
        component: Submitorder,
    }

]


const router = createRouter({
    history: createWebHashHistory(),
    routes
})
// 全局守卫
router.beforeEach((to, form, next) => {
    if (!to.meta.auth) {
        let isLogin = sessionStorage.getItem("token");
        if (!isLogin) {
            router.push("/login");
        } else {
            next();
        }
    } else {
        console.log("正常跳转");
        next();
    }

})


export default router
